#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// 玩家人数范围
#define PLAYER_NUM_MIN  2
#define PLAYER_NUM_MAX  6

// 随机生成怪物生命值范围
#define MONSTER_HEALTH_MIN 50
#define MONSTER_HEALTH_MAX 100

// 随机生成攻击伤害范围
#define ATTACK_DAMAGE_MIN 10
#define ATTACK_DAMAGE_MAX 20

// 清空输入缓冲区
void wait_for_enter() {
    printf("请按Enter键进行攻击...\n");
    while (getchar() != '\n');  // 等待玩家按下Enter键
}

// 获取玩家数量
int get_number_of_players() {
    int num_players;
    do {
        printf("请输入玩家人数(%d-%d): ", PLAYER_NUM_MIN, PLAYER_NUM_MAX);
        scanf("%d", &num_players);
        while (getchar() != '\n');  // 清空输入缓冲区
        if (num_players < PLAYER_NUM_MIN || num_players > PLAYER_NUM_MAX) {
            printf("玩家人数必须在%d到%d之间！\n", PLAYER_NUM_MIN, PLAYER_NUM_MAX);
        }
    } while (num_players < PLAYER_NUM_MIN || num_players > PLAYER_NUM_MAX);

    return num_players;
}

int main() {
    srand(time(0));
    char continue_game;

    do {
        // 获取玩家人数
        int num_players = get_number_of_players();

        // 随机生成怪物生命值
        int monster_health = MONSTER_HEALTH_MIN + rand() % (MONSTER_HEALTH_MAX - MONSTER_HEALTH_MIN + 1);
        printf("\n新的一局游戏开始！怪物的生命点数为: %d\n", monster_health);

        // 记录玩家攻击次数
        int player_hits[num_players];
        for (int i = 0; i < num_players; i++) {
            player_hits[i] = 0;
        }

        int current_player = 0;

        // 游戏循环
        while (monster_health > 0) {
            printf("\n玩家 %d 的回合！当前怪物生命: %d\n", current_player + 1, monster_health);

            // 等待玩家攻击
            wait_for_enter();

            // 生成随机攻击伤害
            int attack_damage = ATTACK_DAMAGE_MIN + rand() % (ATTACK_DAMAGE_MAX - ATTACK_DAMAGE_MIN + 1);
            printf("玩家 %d 攻击了怪物，造成了 %d 点伤害！\n", current_player + 1, attack_damage);

            // 减少怪物生命点数
            monster_health -= attack_damage;

            // 增加玩家攻击次数
            player_hits[current_player]++;

            // 换到下一个玩家
            current_player = (current_player + 1) % num_players;
        }

        printf("\n怪物被击败！\n");

        // 输出每个玩家的攻击次数并判断是否有相同攻击次数的玩家
        int min_hits = player_hits[0];
        int winner = 0;
        int tie = 1;  // 用于检测是否所有玩家攻击次数相同
        int same_hits[num_players];  // 记录攻击次数相同的玩家
        int same_hits_count = 0;     // 相同攻击次数玩家计数

        for (int i = 0; i < num_players; i++) {
            printf("玩家 %d 攻击了 %d 次。\n", i + 1, player_hits[i]);

            if (player_hits[i] < min_hits) {
                min_hits = player_hits[i];
                winner = i;
                same_hits_count = 0;  // 如果找到比当前少的次数，重置相同计数
                tie = 0;              // 不是平局
            }

            if (player_hits[i] == min_hits) {
                same_hits[same_hits_count++] = i + 1;  // 记录相同次数玩家编号
            } else {
                tie = 0;  // 如果有不同的攻击次数，就不是平局
            }
        }

        // 检查并输出相同攻击次数玩家
        if (same_hits_count > 1) {
            printf("以下玩家攻击次数相同：");
            for (int i = 0; i < same_hits_count; i++) {
                printf("玩家 %d ", same_hits[i]);
            }
            printf("\n");
        }

        // 检查是否所有玩家攻击次数相同
        if (tie) {
            printf("平局！所有玩家攻击次数相同。\n");
        } else {
            printf("玩家 %d 获胜！\n", winner + 1);
        }

        // 是否继续游戏
        printf("\n是否继续下一局游戏？(y/n): ");
        scanf(" %c", &continue_game);
        while (getchar() != '\n');  // 清空输入缓冲区

    } while (continue_game == 'y' || continue_game == 'Y');  // 若选择y或Y，则继续游戏

    printf("游戏结束，感谢参与！\n");
    
    return 0;
}



